package com.lxb.myforum.controller

import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.data.domain.Sort.Direction
import org.springframework.data.domain.Sort.Order
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.pojo.Post
import com.lxb.myforum.pojo.Reply
import com.lxb.myforum.pojo.User
import com.lxb.myforum.service.PostService
import com.lxb.myforum.service.ReplyService

@Controller
@RequestMapping("/posts")
class PostController @Autowired constructor(
        private val postService: PostService,
        private val replyService: ReplyService
) : BaseController() {

    @GetMapping(value = ["", "/", "/index"])
    fun index(req: HttpServletRequest, post: Post,
              @RequestParam(value = "page", defaultValue = "1") page: Int,
              @RequestParam(value = "size", defaultValue = "10") size: Int): String {
        val pageable = PageRequest.of(page - 1, size)
        val postsPage = postService.findPosts(post, pageable)

        req.setAttribute("posts", postsPage.content)
        req.setAttribute("totalCount", postsPage.totalElements)
        req.setAttribute("totalPage", postsPage.totalPages)
        req.setAttribute("curPage", postsPage.number + 1)
        req.setAttribute("curSize", postsPage.size)

        return "/posts/index"
    }


    @GetMapping(value = ["/{id}", "/{id}/detail"])
    fun detail(req: HttpServletRequest,
               @PathVariable("id") id: Long,
               @RequestParam(value = "page", defaultValue = "1") page: Int,
               @RequestParam(value = "size", defaultValue = "10") size: Int): String {
        val post = postService.findById(id)
        req.setAttribute("post", post)

        val o = Order(Direction.DESC, "createTime")
        val pageable = PageRequest.of(page - 1, size, Sort.by(o))
        val reply = Reply()
        reply.post = post
        val repliesPage = replyService.findRelies(reply, pageable)

        req.setAttribute("replies", repliesPage.content)
        req.setAttribute("totalCount", repliesPage.totalElements)
        req.setAttribute("totalPage", repliesPage.totalPages)
        req.setAttribute("curPage", repliesPage.number + 1)
        req.setAttribute("curSize", repliesPage.size)

        return "/posts/detail"
    }


    @PostMapping(value = ["/{postId}/replies"])
    @ResponseBody
    fun addReply(req: HttpServletRequest,
                 @PathVariable("postId") postId: Long, reply: Reply): Reply {
        val o = SecurityContextHolder.getContext().authentication.principal
        if (o is User) {
            reply.user = o
        }
        val post = Post(postId)
        reply.post = post
        return replyService.saveReply(reply)
    }

    @GetMapping(value = ["/edit", "/write"])
    fun edit(): String {
        return "/posts/edit"
    }

    @RequestMapping(value = ["", "/"], method = [RequestMethod.POST, RequestMethod.PUT])
    fun save(post: Post): String {
        val o = SecurityContextHolder.getContext().authentication.principal
        if (o is User) {
            post.user = o
        }
        val (id) = postService.savePost(post)
        return "redirect:/posts/" + id!!
    }
}
